package com.example.c17310120213_case38_menu_list;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import javax.xml.transform.Templates;

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<FoodBean>list;
    private Context context;
    public MyAdapter(Context context,List<FoodBean>list) {
        this.list=list;
        this.context = context;

    }



    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.item,parent,false);
        return new ViewHolder(v);


    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
    FoodBean foodBean = list.get(position);
    if(foodBean==null)
        return;
    ViewHolder viewHolder = (ViewHolder)holder;
    viewHolder.imageView.setImageResource(foodBean.getPictureId());
    viewHolder.dish_name.setText(foodBean.getDish_name());
    viewHolder.flavor.setText(foodBean.getFlavor());
    viewHolder.description.setText(foodBean.getDescription());
    viewHolder.textView4.setText(foodBean.getNum());//销量
    viewHolder.textView5.setText(foodBean.getTime());


    viewHolder.imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String str = String.valueOf(list.get(position).getDescription());

            Toast.makeText(context,str,Toast.LENGTH_SHORT).show();
        }
    });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }



    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView dish_name,flavor,description,textView4,textView5;
        Button button;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.imageView);
            dish_name = itemView.findViewById((R.id.textView));
            flavor = itemView.findViewById(R.id.textView2);
            description = itemView.findViewById(R.id.textView3);
            textView4 = itemView.findViewById(R.id.textView4);
            textView5 = itemView.findViewById(R.id.textView5);
            button = (Button)itemView.findViewById(R.id.button);
        }
    }
}
